class Solution {
public:
    vector<int> dailyTemperatures(vector<int>& temperatures) {
        int n = temperatures.size();
        vector<int> st(n);
        vector<int> ans(n);
        int r = 0;
        for (int i = 0; i < n; i++)
        {
            while (r > 0 && temperatures[st[r - 1]] < temperatures[i])
            {
                int cur = st[--r];
                ans[cur] = i - cur;
            }
            st[r++] = i;
        }
        return ans;
    }
};